// 画框和拖拽控制点改变位置大小相关逻辑
// pointerdown 传入相关变量定制 pointermove 触发调用的数据变化函数

/**
 * 拖拽计算后的点再校准回限定范围
 * @param {(side: 'width' | 'height') => [number, number]} calcRange
 */
export const CustomPosLimit = calcRange => R.mapObjIndexed(
  (v, side) => R.compose( R.apply(R.clamp(R.__, R.__, v)), calcRange )(side)
);

// 比如右上点活跃，对立控制点为左下，右上控制点触发 pointerdown，赋值给 startPoint
// stage 触发 pointermove
// drawRect 所需 topLeft 为 { x: Math.min(活跃.x, 对立.x), y: Math.min(活跃.y, 对立.y) }
// 拖拽单方向控制点更新活跃控制点位置，只更新指定方向上的
export const calcPosAndSize = oppoPos => activePos => {
  const calcTopLeft = (p1, p2) => ({ x: Math.min(p1.x, p2.x), y: Math.min(p1.y, p2.y) });
  const calcSize = (p1, p2) => ({ width: Math.abs(p1.x - p2.x), height: Math.abs(p1.y - p2.y) });
  
  return Object.assign(calcTopLeft(activePos, oppoPos), calcSize(activePos, oppoPos));
};

const calcActivePos = (startPoint, toChange) => global => Object.assign({}, startPoint, R.pick(toChange, global));

export const DragCtrlPointListener = ({ startPoint, calcGlobalPoint, toChange = ['x', 'y'] }) => R.compose(
  /** limitTo */
  calcActivePos(startPoint, toChange),
  calcGlobalPoint,
);
